<?php
include "constants.inc.php";
include_once "common.class.php";
class database
{
	private static $db_connection;

	function __construct()
	{
		// Connect to Database
		$this->db_connection = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD) or common::error(mysql_error());
		mysql_select_db(DB_NAME,$this->db_connection) or common::error(mysql_error());
	}

	// run a query, and return the result resource
	function query($sql)
	{
		$err_sql = (DB_DEBUG) ? "<br><br>".$sql : "";
		@$result = mysql_query($sql) or common::error(mysql_error().$err_sql,__FILE__,__LINE__);
		return $result;
	}

	// return a single record object
	function get_record($sql)
	{
		$err_sql = (DB_DEBUG) ? "<br><br>".$sql : "";
		$result = mysql_query($sql) or common::error(mysql_error().$err_sql,__FILE__,__LINE__);
		if ($row = mysql_fetch_object($result))
		{
			return $row;
		}
		else
		{
			//return false;
		}

	}

	// return a single record as an array (associative)
	function get_record_array($sql)
	{
		$err_sql = (DB_DEBUG) ? "<br><br>".$sql : "";
		$result = mysql_query($sql) or common::error(mysql_error().$err_sql,__FILE__,__LINE__);
		if ($row = mysql_fetch_assoc($result))
		{
			return $row;
		}
		else
		{
			return false;
		}
	}

	// return an array of record objects
	function get_records($sql)
	{
		$err_sql = (DB_DEBUG) ? "<br><br>".$sql : "";
		$result = mysql_query($sql) or common::error(mysql_error().$err_sql,__FILE__,__LINE__);
		$records = array();
		while ($row = mysql_fetch_object($result))
		{
			array_push($records, $row);
		}
		mysql_free_result($result);
		return $records;
	}
	
	function curPageURL()
	{
		$pageURL = 'http';
		//if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
			$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80")
		{
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		}
		else
		{
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}
}
?>